package com.ecoolex.chinadutyfree.retailer.core.manager;

import java.sql.Date;
import java.time.Instant;
import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.ecoolex.chinadutyfree.retailer.core.dao.GoodsSpecDAO;
import com.ecoolex.chinadutyfree.retailer.entity.GoodsSpec;
import com.ecoolex.chinadutyfree.retailer.entity.GoodsSpec.Column;
import com.ecoolex.chinadutyfree.retailer.entity.GoodsSpecExample;
import com.ecoolex.chinadutyfree.retailer.entity.GoodsSpecGroup;
import com.ecoolex.chinadutyfree.retailer.pojo.EditSpecDTO;
import com.ecoolex.framework.common.enums.ResultCode;
import com.ecoolex.framework.common.util.BizAssert;
import com.ecoolex.framework.common.util.Check;
import com.ecoolex.framework.common.util.CollectorUtil;


@Service
public class GoodsSpecManager {

	@Autowired
	private GoodsSpecDAO dao;

	public void verify(EditSpecDTO input) {
		BizAssert.isTrue(Check.notNullOrTrimEmpty(input.getName()), ResultCode.VERIFY.build(41), "规格名称为空");
	}

	public void update(GoodsSpecGroup group, List<EditSpecDTO> specList, String updateUser) {
		List<GoodsSpec> oldList = queryByGroupId(group.getSpecGroupId());

		for (int i = 0, size = specList.size(); i < size; i++) {
			EditSpecDTO input = specList.get(i);
			if (Check.notNull(input.getSpecId())) {
				GoodsSpec old
					= oldList.stream().collect(CollectorUtil.first(s -> input.getSpecId().equals(s.getSpecId())));
				BizAssert.isTrue(Check.notNull(
					old), ResultCode.VERIFY.build(40), "规格分组={}下规格值={}不存在", group.getName(), input.getSpecId());

				old.setName(input.getName());
				old.setSorting(i + 1);
				old.setUpdateUser(updateUser);
				old.setUpdateTime(Date.from(Instant.now()));
				dao.updateByPrimaryKeySelective(old, Column.name, Column.sorting, Column.updateUser, Column.updateTime);
				// 移除旧的数组
				oldList.remove(old);
			} else {
				GoodsSpec spec = new GoodsSpec();
				spec.setSpecGroupId(group.getSpecGroupId());
				spec.setName(input.getName());
				spec.setSorting(i + 1);
				spec.setDeleted(GoodsSpec.NOT_DELETED);
				spec.setCreateUser(updateUser);
				spec.setCreateTime(Date.from(Instant.now()));
				spec.setUpdateUser(updateUser);
				spec.setUpdateTime(Date.from(Instant.now()));
				dao.insert(spec);
				// 设置新增规格值的id,以备规格时使用
				input.setSpecId(spec.getSpecId());
			}
		}

		oldList.forEach(old -> {
			old.setDeleted(GoodsSpec.IS_DELETED);
			old.setUpdateUser(updateUser);
			old.setUpdateTime(Date.from(Instant.now()));
			dao.updateByPrimaryKeySelective(old, Column.deleted, Column.updateUser, Column.updateTime);
		});
	}

	public List<GoodsSpec> queryByGroupId(Integer groupId) {
		GoodsSpecExample example = new GoodsSpecExample();
		example.createCriteria().andSpecGroupIdEqualTo(groupId).andDeletedEqualTo(GoodsSpec.NOT_DELETED);
		example.orderBy(Column.sorting.asc());
		return dao.selectByExample(example);
	}

	public void deleteByGroupId(Integer groupId, String updateUser) {
		GoodsSpecExample example = new GoodsSpecExample();
		example.createCriteria().andSpecGroupIdEqualTo(groupId).andDeletedEqualTo(GoodsSpec.NOT_DELETED);

		GoodsSpec record = new GoodsSpec();
		record.setDeleted(GoodsSpec.IS_DELETED);
		record.setUpdateUser(updateUser);
		record.setUpdateTime(Date.from(Instant.now()));

		dao.updateByExampleSelective(record, example, Column.deleted, Column.updateUser, Column.updateTime);
	}

}